#include <iostream>
#include <fstream>
#include <string>
#include "person.pb.h"

using namespace std;

// 提示用户输入一个Person的信息
void PromptForAddress(example::Person* person) {
    cout << "Enter person ID number: ";
    int id;
    cin >> id;
    person->set_id(id);
    cin.ignore(256, '\n');

    cout << "Enter name: ";
    getline(cin, *person->mutable_name());

    cout << "Enter email address (blank for none): ";
    string email;
    getline(cin, email);
    if (!email.empty()) {
        person->set_email(email);
    }

    while (true) {
        cout << "Enter a phone number (or leave blank to finish): ";
        string number;
        getline(cin, number);
        if (number.empty()) {
            break;
        }

        example::Person::PhoneNumber* phone_number = person->add_phones();
        phone_number->set_number(number);

        cout << "Is this a mobile, home, or work phone? ";
        string type;
        getline(cin, type);
        if (type == "mobile") {
            phone_number->set_type(example::Person::MOBILE);
        } else if (type == "home") {
            phone_number->set_type(example::Person::HOME);
        } else if (type == "work") {
            phone_number->set_type(example::Person::WORK);
        } else {
            cout << "Unknown phone type. Using default." << endl;
        }
    }
}

// 遍历AddressBook中的所有Person并打印信息
void ListPeople(const example::AddressBook& address_book) {
    for (int i = 0; i < address_book.people_size(); i++) {
        const example::Person& person = address_book.people(i);

        cout << "Person ID: " << person.id() << endl;
        cout << "  Name: " << person.name() << endl;
        if (!person.email().empty()) {
            cout << "  E-mail address: " << person.email() << endl;
        }

        for (int j = 0; j < person.phones_size(); j++) {
            const example::Person::PhoneNumber& phone_number = person.phones(j);

            switch (phone_number.type()) {
                case example::Person::MOBILE:
                    cout << "  Mobile phone #: ";
                    break;
                case example::Person::HOME:
                    cout << "  Home phone #: ";
                    break;
                case example::Person::WORK:
                    cout << "  Work phone #: ";
                    break;
            }
            cout << phone_number.number() << endl;
        }
    }
}

int main(int argc, char* argv[]) {
    // 验证正确的参数数量
    if (argc != 2) {
        cerr << "Usage:  " << argv[0] << " ADDRESS_BOOK_FILE" << endl;
        return -1;
    }

    example::AddressBook address_book;

    {
        // 读取现有的address book
        fstream input(argv[1], ios::in | ios::binary);
        if (!input) {
            cout << argv[1] << ": File not found.  Creating a new file." << endl;
        } else if (!address_book.ParseFromIstream(&input)) {
            cerr << "Failed to parse address book." << endl;
            return -1;
        }
    }

    // 添加一个地址
    PromptForAddress(address_book.add_people());

    {
        // 写入新的address book到磁盘
        fstream output(argv[1], ios::out | ios::trunc | ios::binary);
        if (!address_book.SerializeToOstream(&output)) {
            cerr << "Failed to write address book." << endl;
            return -1;
        }
    }

    // 可选：删除libprotobuf分配的所有全局对象
    google::protobuf::ShutdownProtobufLibrary();

    return 0;
}